package com.intelligentcar.springboot.service.serverImpl;

import com.intelligentcar.springboot.bean.Manager;
import com.intelligentcar.springboot.mapper.LogMapper;
import com.intelligentcar.springboot.mapper.ManagerMapper;
import com.intelligentcar.springboot.service.LogServer;
import com.intelligentcar.springboot.service.ManagerServer;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ManagerServerImpl implements ManagerServer {
    @Autowired
    private LogMapper logMapper;
    @Autowired
    private ManagerMapper managerMapper;
    @Autowired
    private Md5 md5;

    //查询所有的维修人员
    @Override
    public List<Manager> queryrepair() {
        return managerMapper.queryrepair();
    }

    //新增后台人员
    @Override
    public int manageradd(Manager manager) {
        return managerMapper.manageradd(manager);
    }

    //查询后台人员有几条数据
    @Override
    public int count() {
        return managerMapper.count();
    }

    @Override
    public int count1(Manager manager) {
        return managerMapper.count1(manager);
    }

    @Override
    public int count2(Manager manager) {
        return managerMapper.count2(manager);
    }

    @Override
    public int count3(Manager manager) {
        return managerMapper.count3(manager);
    }

    //查询后台人员表所有数据
    @Override
    public List<Manager> selectmanager(RowBounds rowBounds) {
        return managerMapper.selectmanager(rowBounds);
    }

    //修改后台人员
    @Override
    public int update(Manager manager) {
        return managerMapper.update(manager);
    }

    //删除后台人员
    @Override
    public int delete(Manager manager) {
        return managerMapper.delete(manager);
    }

    //查询后台人员
    @Override
    public List<Manager> querybackstage(Manager manager, RowBounds rowBounds) {
        return managerMapper.querybackstage(manager, rowBounds);
    }

    //查询后台人员
    @Override
    public List<Manager> querybackstage1(Manager manager, RowBounds rowBounds) {
        return managerMapper.querybackstage1(manager, rowBounds);
    }

    //查询后台人员
    @Override
    public List<Manager> querybackstage2(Manager manager, RowBounds rowBounds) {
        return managerMapper.querybackstage2(manager, rowBounds);
    }

    //查询后台人员
    @Override
    public List<Manager> querybackstage3(Manager manager) {
        return managerMapper.querybackstage3(manager);
    }


    @Override
    public Map<String, Object> login(HttpServletRequest req, Manager manager) {
        Map<String, Object> map = new HashMap<>();
        int result = 0;
        manager.setUpwd(md5.MD5(manager.getUpwd()));
        Manager manager1 = managerMapper.login(manager);
        try {
            if (manager1.getUId() != 0) {
                if (manager1.getUstate().equals("启用")) {
                    req.getSession().setAttribute("userName", manager1.getUname());

//                    System.out.println("1111111111111111111111111");
                    //日志
                    String time = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss").format(new Date());
                    String logUser = req.getSession().getAttribute("userName").toString();
                    logMapper.addLog(time, "登录", logUser, "账号登录");
                    result = 1;
                } else {
                    result = 2;
                }
            } else {
                result = 3;
            }
        } catch (Exception e) {
            result = 3;
        }
        map.put("mangerList", manager1);
        map.put("result", result);
        return map;
    }

    @Override
    public boolean updateSelf(HttpServletRequest req, Manager manager) {
//        String time = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss").format(new Date());
//        String logUser = req.getSession().getAttribute("userName").toString();
//        logMapper.addLog(time, "修改", logUser, "修改个人信息");
        int i = managerMapper.updateSelf(manager);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updatePwd(Manager manager) {
        manager.setUpwd(md5.MD5(manager.getUpwd()));
        int i = managerMapper.updatePwd(manager);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public int updateHead(Manager manager) {
        return managerMapper.updateHead(manager);
    }

    @Override
    public Manager selectInfor(Manager manager) {
        Manager manager1 = managerMapper.selectInfor(manager);
        return managerMapper.selectInfor(manager);
    }

    @Override
    public boolean pwdCheck(Manager manager) {
        manager.setUpwd(md5.MD5(manager.getUpwd()));
        Manager manager1 = managerMapper.login(manager);
        if(manager1 == null){
            return false;
        }
        return true;
    }

}
